// pages/dontforget/dontforget.js
const db = wx.cloud.database();

Page({
  data: {
    dormitoryId: 0,
    memos: [],
    showAddMemoDialog: false,
    inputTitle: '',
    selectedDate: '',
  },

  onLoad: function () {
    const app = getApp(); // 获取小程序实例
    const dormitoryId = app.globalData.dormitoryId;
    console.log(dormitoryId);
    this.setData({
      dormitoryId: app.globalData.dormitoryId
    })
    this.refreshMemos();
  },

  refreshMemos: function () {
    // 获取数据库中所有备忘录信息
    db.collection("dontforget").where({
      dormitoryId: this.data.dormitoryId
    }).get().then(res => {

      this.setData({
        memos: res.data
      });
    });
  },

  toggleMemoCompletion(event) {
    const index = event.currentTarget.dataset.index;
    const memos = this.data.memos;

    // 切换备忘录的完成状态
    memos[index].completed = !memos[index].completed;

    this.setData({
      memos: memos
    });

    // 更新后台数据库中备忘录的完成状态
    const selectedMemo = this.data.memos[index];
    db.collection('dontforget').doc(selectedMemo._id).update({ data: { completed: selectedMemo.completed }})
      .then(res => {
        console.log('备忘录完成状态更新成功', res);
      })
      .catch(err => {
        console.error('备忘录完成状态更新失败', err);
      });
  },



  onShow: function () {
    const app = getApp(); // 获取小程序实例
    const dormitoryId = app.globalData.dormitoryId;
    console.log(dormitoryId);
    this.setData({
      dormitoryId: app.globalData.dormitoryId
    })
    this.refreshMemos();
  },

  showAddMemoDialog: function () {
    this.setData({
      showAddMemoDialog: true,
    });
  },

  hideAddMemoDialog: function () {
    this.setData({
      showAddMemoDialog: false,
      inputTitle: '',
      selectedDate: '',
    });
  },

  hideAddMemoDialog: function () {
    this.setData({
      showAddMemoDialog: false,
      inputTitle: '',
      selectedDate: '',
    });
  },

  preventTap: function (event) {
    return false;
  },

  hideAddMemoDialog: function () {
    this.setData({
      showAddMemoDialog: false,
      inputTitle: '',
      selectedDate: '',
    });
  },

  inputTitle: function (event) {
    const value = event.detail.value;
    this.setData({
      inputTitle: value,
    });
  },

  bindDateChange: function (e) {
    this.setData({
      selectedDate: e.detail.value,
    });
  },

  addMemo: function () {
    const title = this.data.inputTitle;
    const date = this.data.selectedDate;
    
    if (!title || !date) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none',
      });
      return;
    }

    const newMemo = {
      title: title,
      date: date,
      dormitoryId: this.data.dormitoryId, // 在新备忘录中包含 dormitoryId
    };

    // 将新备忘录加入备忘录列表
    this.setData({
      memos: [...this.data.memos, newMemo],
    });

    // 添加完毕后，关闭弹窗
    this.hideAddMemoDialog();

    // 保存到数据库
    db.collection('dontforget').add({
      data: newMemo,
    }).then(res => {
      // 添加成功后刷新页面
      this.refreshMemos();

      wx.showToast({
        title: '添加成功',
        icon: 'success',
      });
    }).catch(error => {
      console.error('添加失败', error);
      wx.showToast({
        title: '添加失败',
        icon: 'none',
      });
    });
  },

  deleteMemo: function (e) {
    const memoId = e.currentTarget.dataset.id;
    const selectedMemo = this.data.memos.find(memo => memo._id === memoId);

    db.collection('dontforget').doc(selectedMemo._id).remove().then(() => {
      wx.showToast({
        title: '删除成功',
        icon: 'success',
      });
      this.refreshMemos();
    }).catch(error => {
      console.error('删除失败', error);
      wx.showToast({
        title: '删除失败',
        icon: 'none',
      });
    });

  },
});